package com.sblab.appengine.service;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.google.appengine.api.utils.SystemProperty;

public final class EMF {
	private static final EntityManagerFactory emfInstance;
	
	static {
		Map<String, String> properties = new HashMap();
		if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
			properties.put("javax.persistence.jdbc.driver", System.getProperty("cloudsql.driver"));
			properties.put("javax.persistence.jdbc.url", System.getProperty("cloudsql.url"));
			properties.put("javax.persistence.jdbc.user", System.getProperty("cloudsql.user"));
		} else {
			properties.put("javax.persistence.jdbc.driver", System.getProperty("cloudsql.dev.driver"));
			properties.put("javax.persistence.jdbc.url", System.getProperty("cloudsql.dev.url"));
			properties.put("javax.persistence.jdbc.user", System.getProperty("cloudsql.dev.user"));
			properties.put("javax.persistence.jdbc.password", System.getProperty("cloudsql.dev.password"));
		}

		emfInstance = Persistence.createEntityManagerFactory("Demo", properties);
	}
    
    private EMF() {}

    public static EntityManagerFactory get() {
        return emfInstance;
    }
}

